<template>
    <van-nav-bar title="订单预览" :fixed="true" left-arrow @click-left="router.back()"/>
    <!-- 选择地址 -->
    <div class="mt46 addressBox" @click="router.push('/address')">
      <div class="info">
        <p class="user">
          <span>{{addressData.name}}</span>
          <span>{{addressData.phone}}</span>
        </p>
        <p class="address">
          <span>{{addressData.province}}</span>
          <span>{{addressData.city}}</span>
          <span>{{addressData.county}}</span>
          <span>{{addressData.address}}</span>
        </p>
      </div>
      <van-icon name="arrow" size="22" />
    </div>
    <!-- 购物车列表 -->
    <div class="goodsList">
      <van-card
      v-for="item in cartData"
      >
        <template #title>
          <div class="title">
            <p>{{item.goods.title}}</p>
            <p class="num">x{{item.num}}</p>
          </div>
        </template>
        <template #thumb>
          <img :src="item.goods.cover_url">
        </template>
        <template #bottom>
          <span><small>&yen;</small>{{item.goods.price}}</span>
        </template>
      </van-card>
    </div>
    <!-- 底部结算 -->
    <van-submit-bar :price="store.countPrice" button-text="生成订单" @submit="onSubmit">
    </van-submit-bar>
  </template>
      
  <script setup lang='ts'>
  import { onMounted } from 'vue';
  import { useRouter } from 'vue-router';
  import { referOrders } from '@/api/cart';
  
  import { useOrderStore } from '@/store/orderStore'
  import { storeToRefs } from 'pinia';
import { Toast } from 'vant';
  
    const router = useRouter()
    // 使用 pinia
    let store = useOrderStore()
    let { cartData,addressData } = storeToRefs(store)
  
    const onSubmit = async () => {
      let res = await referOrders(store.addressData.id)
      if(res.status === 200){
        Toast.success("成功生成订单")
        router.push({name: 'orderDetail',params:{id:res.data.id}})
      }else if(res.status === 400){
        Toast.fail("库存不足，请重新选择商品")
      }else if(res.status === 422){
        Toast.fail("收货地址不能为空")
      }else{
        Toast.fail("生成订单失败！")
      }
    }
    // 跳转至地址列表
    onMounted(() => {
      store.getCreateOrder()
    })
  </script>
      
<style scoped lang="less">
.addressBox {
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 15px;
  border-bottom: 3px solid skyblue;
  .info {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: flex-start;
    span {
      margin-right: 5px;
    }
    .user {
      margin-bottom: 10px;
      span {
        &:first-of-type {
          margin-right: 20px;
        }
      }
    }
  }
}
.goodsList {
  img {
    width: 100px;
    height: 100px;
  }
  .title {
    display: flex;
    justify-content: space-between;
    font-size: 16px;
    color: #2c3e50;
    margin-left: -10px !important;
  }
  /deep/ .van-card {
    background-color: #fff;
    .van-card__thumb {
      width: 100px;
    }
    .van-card__content {
      padding: 10px;
      box-sizing: border-box;
      .num {
        margin-top: 5px;
      }
      .van-card__bottom {
        display: flex;
        justify-content: space-between;
        align-items: center;
        span {
          font-size: 16px;
          color: red;
        }
      }
    }
  }
}
.van-submit-bar {
  bottom: 50px;
}
</style>